home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / backup / dds2tar-.000 / dds2tar-2.4.12.tar / dds2tar-2.4.12 / dds_index.c < prev    next >
C/C++ Source or Header  |  1996-01-28  |  4KB  |  204 lines

  1.  
  2. /*
  3.  * This file is part of dds2tar.
  4.  * Copyright by J"org Weule
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <sys/mtio.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include "dds2tar.h"
  13. #include "dds_tape.h"
  14.  
  15. /********
  16.     * Converting a tar-record to an entry in the index file.
  17.     *
  18.     *  input: tar_record is the header of one file in the archive.
  19.         *         The location of the header is given by the
  20.     *         block and index value.
  21.     * output: The returned index is the location of the next header.
  22.     *         Normalization of the pair (block,rec) with respect to
  23.     *         the block length will give the right position.
  24.     * extern: index_fp, verbose
  25.     *         index_fp is the FILE to write the rec.
  26.     *         verbose indicates the loggin mode.
  27.     *
  28.     *         If the name length of the file name is zero,
  29.     *         and an empty block is found, the end
  30.     *         of the archive is found and the program stops.
  31.     *
  32.     ********************************************************************/
  33. static int
  34. dds_index_h(tar_record *const ptr, int const block, int rec)
  35. {
  36.     int     size;
  37.     char   *p = ptr->hdr.magic;
  38.  
  39. #ifdef DDS_TRACE
  40.     fputs("dds_index_h()\n", stderr);
  41. #endif
  42.  
  43.     if ( dds_is_tar_header_record(ptr) == 0 ) {
  44.         fprintf(stderr,"dds2index: skipping to next header\n");
  45.         return ( rec + 1 ) ;
  46.     }
  47.  
  48.     if (*p == 0)
  49.         p = "magic ";
  50.     if (ptr->hdr.linkflag == 'V') p = "label ";
  51.     else if (p[6] == ' ')
  52.         p[6] = 0;
  53.     if (ptr->hdr.name[0] == 0) {
  54.         int     i = 0;
  55.  
  56.         while ((((char *) ptr)[i] == 0) && (i < 512))
  57.             i++;
  58.         if (i == 512) {
  59.             fprintf(index_fp, dds_index_format,
  60.                 "-end- ",
  61.                 block,
  62.                 rec,
  63.                 0, "(null)");
  64.             close(device);
  65.             exit(0);
  66.         }
  67.     }
  68.     if (ptr->hdr.size[0] != 0)
  69.         sscanf(ptr->hdr.size, "%o", &size);
  70.     else
  71.         size = 0;
  72.     ptr->hdr.name[100] = 0;
  73.     sprintf(cur_line, dds_index_format,
  74.         p,
  75.         block,
  76.         rec,
  77.         size,
  78.         ptr->hdr.name
  79.         );
  80.     fputs(cur_line, index_fp);
  81.     if (verbose)
  82.         fputs(cur_line, stderr);
  83.     /* calculate the number of records */
  84.     /* there was one header record */
  85.     size += 511 + 512;
  86.     size >>= 9;
  87.     rec += size;
  88.     return rec;
  89. }
  90.  
  91. /********
  92.         * Converting a table line to the numbers.
  93.         *
  94.         */
  95. int
  96. rt_line(
  97.            int *const ptr_blkno,
  98.            int *const ptr_recno,
  99.            int *const ptr_size,
  100.            char **const ptr_name
  101. )
  102. {
  103.     char    p[16];
  104.     int     i;
  105.     char    const *dds_fmt = NULL;
  106.  
  107. #ifdef DDS_TRACE
  108.     fprintf(stderr,"rt_line(...)\n");
  109. #endif
  110.  
  111.     if (cur_line[16] == ':') {
  112.         dds_fmt = dds_old_index_scan_format ;
  113.         *ptr_name = cur_line + 27;
  114.     } else {
  115.         if (cur_line[17] == ':') {
  116.             dds_fmt = dds_index_scan_format ;
  117.             *ptr_name = cur_line + 28;
  118.         } else return 1 ;
  119.     } ;
  120.  
  121.     if ((i = sscanf(cur_line,
  122.             dds_fmt,
  123.             p,
  124.             ptr_blkno,
  125.             ptr_recno,
  126.             ptr_size)) < 4)
  127.         fprintf(stderr, "Wrong line inside the index file,"
  128.             " scanned %d values\n", i);
  129. #ifdef DDS_TRACE
  130.     fprintf(stderr,"end >> rt_line(...)\n");
  131. #endif
  132.     return 0 ;
  133. }
  134.  
  135. /* procedure to create the index table */
  136. /********
  137.     *  input: none
  138.     * output: none
  139.     * extern: device, index_fp
  140.     *
  141.     * The archive is read, and the index file is written.
  142.     *
  143.     ************************************************************/
  144.  
  145. int
  146. dds_index(void)
  147. {
  148.  
  149.     int     cur_rec;
  150.     int     cur_byte;
  151.     static const char hash_sign = '#';
  152.  
  153. #ifdef DDS_TRACE
  154.     fputs("dds_index()\n", stderr);
  155. #endif
  156.  
  157.  
  158.     fputs(dds_headline, index_fp);
  159.     if (verbose) fputs(dds_headline, index_fp);
  160.     next_blkno = -1 ;
  161.     cur_rec = 0;
  162.     dds_read_block();
  163.     cur_byte = cur_n;
  164.     if (hash_mode == 1)
  165.         if ((cur_blkno & 0x1f) == 0) {
  166.             write(2, &hash_sign, 1);
  167.         }
  168.     while (cur_n > 0) {
  169.         cur_bs = cur_n >> 9;
  170.         while (cur_rec < cur_bs)
  171.             cur_rec = dds_index_h(
  172.                              cur_block + cur_rec,
  173.                              cur_blkno,
  174.                              cur_rec
  175.                 );
  176.         cur_rec -= cur_bs;
  177.  
  178.         /*
  179.          * The hp-DAT is slowed down with dds_getpos().
  180.                  * Is there a reason for this?
  181.          *
  182.          * Now the cur_blkno is incremented.
  183.          */
  184.         dds_read_next_block();
  185.         cur_byte += cur_n;
  186.         if (hash_mode == 1)
  187.             if ((cur_blkno & 0x1f) == 0) {
  188.                 if ((cur_blkno & 0x7ff) == 0) {
  189.                     fprintf(stderr, " %d\n",
  190.                         cur_byte);
  191.                     fflush(stderr);
  192.                 }
  193.                 write(2, &hash_sign, 1);
  194.             }
  195.     }
  196.     if (cur_n <= 0) {
  197.         perror("dds2tar");
  198.         fprintf(stderr, "dds_index: unexpected end of tar archive \n");
  199.         close(device);
  200.         exit(9);
  201.     }
  202.     return 0;
  203. }
  204.